"use strict";

$(function () {
    "use strict";

    // 详情页面开始

    var anim = $(".anim span");
    var previewImg = $(".preview img");
    var BigpreviewImg = $(".big-preview img");
    var pro_h1 = $(".pro-hd h1");
    var pro_p = $(".pro-hd p");
    var mod_price = $(".pro-sell .mod-price");

    anim.html($.cookie("name"));
    previewImg.attr("src", $.cookie("img"));
    BigpreviewImg.attr("src", $.cookie("img"));
    pro_h1.html($.cookie("name"));
    pro_h1.attr("goodsId", $.cookie("id"));
    pro_p.html($.cookie("desc"));
    mod_price.html($.cookie("price"));

    var preview = $(".preview");
    var previewSpan = $(".preview span");
    var Bigpreview = $(".big-preview");
    var Bigimg = $(".big-preview img");

    preview.mouseover(function () {
        // 计算span的值
        var w = Bigpreview.width() / Bigimg.width() * preview.width();
        var h = Bigpreview.height() / Bigimg.height() * preview.height();

        previewSpan.css({ width: w, height: h });
        previewSpan.stop().show(0);
        Bigpreview.stop().show(0);
    });
    preview.mouseout(function () {
        previewSpan.stop().hide(0);
        Bigpreview.stop().hide(0);
    });
    preview.mousemove(function (e) {
        var l = e.pageX - preview.offset().left - previewSpan.width() / 2;
        var t = e.pageY - preview.offset().top - previewSpan.height() / 2;
        // 边界限定
        if (l < 0) l = 0;
        if (t < 0) t = 0;
        if (l > $(this).width() - previewSpan.width()) {
            l = $(this).width() - previewSpan.width();
        }
        if (t > $(this).height() - previewSpan.height()) {
            t = $(this).height() - previewSpan.height();
        }
        // span跟随鼠标
        previewSpan.css({
            "left": l,
            "top": t
        });
        //  根据比例计算右边大图应该移动的距离
        // 比例 * 总值，得到的就是当前应该移动的距离
        var x = l / ($(this).width() - previewSpan.width());
        var y = t / ($(this).height() - previewSpan.height());

        Bigimg.css({
            left: x * (Bigpreview.width() - Bigimg.width()),
            top: y * (Bigpreview.height() - Bigimg.height())
        });
    });

    // 点击加入购物车
    $(".btn-buy").click(function () {
        $(".information").css("display", "block");
        //  加入购物车功能开始
        // 判断是否是第一次存
        var goods = $.cookie("goods") ? JSON.parse($.cookie("goods")) : [];
        // 如果是第一次存，直接添加到数组中
        if (goods.length < 1) {
            goods.push({
                id: pro_h1.attr("goodsId"),
                name: pro_h1.html(),
                img: previewImg.attr("src"),
                desc: pro_p.html(),
                price: mod_price.html(),
                num: 1
            });
            $.cookie("goods", JSON.stringify(goods));
        } else {
            // 如果不是第一次存，增加商品数目
            var i = 0;
            var onoff = goods.some(function (val, index) {
                i = index;
                return val.id === pro_h1.attr("goodsId");
            });
            if (onoff) {
                goods[i].num++;
            } else {
                goods.push({
                    id: pro_h1.attr("goodsId"),
                    name: pro_h1.html(),
                    img: previewImg.attr("src"),
                    desc: pro_p.html(),
                    price: mod_price.html(),
                    num: 1
                });
            }
            // 将处理后的数组再转成字符设置回cookie
            $.cookie("goods", JSON.stringify(goods));
            console.log(goods);
        }
    });
    $(".information span").click(function () {
        $(".information").css("display", "none");
    });

    // 详情页面结束
});